package com.ai.service.impl;

import com.ai.entity.SysDictDetailEntity;
import com.ai.entity.SysDictEntity;
import com.ai.mapper.SysDictDetailMapper;
import com.ai.mapper.SysDictMapper;
import com.ai.service.SysDictService;
import com.ai.utils.AssertUtil;
import com.ai.utils.ConvertUtils;
import com.ai.web.PageResp;
import com.ai.web.request.SysDictAddReq;
import com.ai.web.request.SysDictPageReq;
import com.ai.web.request.SysDictUpdateReq;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据字典服务类
 */
@Service("sysDictService")
public class SysDictServiceImpl  implements SysDictService {

    @Resource
    private SysDictDetailMapper sysDictDetailMapper;

    @Resource
    private SysDictMapper sysDictMapper;
    /**
     * 根据字典类型查询字典数据信息
     *
     * @param name 字典名称
     * @return 参数键值
     **/
    public JSONArray getType(String name) {
        if (StringUtils.isEmpty(name)) {
            return new JSONArray();
        }
        //根据名称获取字典
        SysDictEntity dict = sysDictMapper.selectOne(Wrappers.<SysDictEntity>lambdaQuery().eq(SysDictEntity::getName, name));
        if (dict == null || dict.getId() == null) {
            return new JSONArray();
        }
        //获取明细
        List<SysDictDetailEntity> list = sysDictDetailMapper.selectList(Wrappers.<SysDictDetailEntity>lambdaQuery().eq(SysDictDetailEntity::getDictId, dict.getId()));
        return JSONArray.parseArray(JSON.toJSONString(list));
    }

    @Override
    public void save(SysDictAddReq sysDictAddReq) {
        SysDictEntity q = sysDictMapper.selectOne(Wrappers.<SysDictEntity>lambdaQuery().eq(SysDictEntity::getName, sysDictAddReq.getName()));
        AssertUtil.isNull(q, "字典名称已存在");
        SysDictEntity sysDictEntity = ConvertUtils.convert(sysDictAddReq, SysDictEntity.class);
        sysDictMapper.insert(sysDictEntity);
    }

    @Override
    public void delete(List<String> ids) {
        sysDictMapper.deleteById(Wrappers.<SysDictEntity>lambdaQuery().in(SysDictEntity::getId, ids));
        sysDictDetailMapper.delete(Wrappers.<SysDictDetailEntity>lambdaQuery().in(SysDictDetailEntity::getDictId, ids));
    }

    @Override
    public void updateById(SysDictUpdateReq sysDictUpdateReq) {
        SysDictEntity dict = new SysDictEntity();
        dict.setId(sysDictUpdateReq.getId());
        dict.setName(sysDictUpdateReq.getName());
        dict.setRemark(sysDictUpdateReq.getRemark());
        sysDictMapper.updateById(dict);
    }

    @Override
    public PageResp<SysDictEntity> page(SysDictPageReq sysDictPageReq) {
        LambdaQueryWrapper<SysDictEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        if (!StringUtils.isEmpty(sysDictPageReq.getName())) {
            queryWrapper.like(SysDictEntity::getName, sysDictPageReq.getName());
            queryWrapper.or();
            queryWrapper.like(SysDictEntity::getRemark, sysDictPageReq.getName());
        }
        queryWrapper.orderByAsc(SysDictEntity::getName);

        IPage<SysDictEntity> iPage = sysDictMapper.selectPage(new Page<>(sysDictPageReq.getPage(), sysDictPageReq.getLimit()), queryWrapper);
        return ConvertUtils.convertPageResp(iPage);
    }

}